package csu.web.credit_bank.service.impl;

import csu.web.credit_bank.service.ImageService;
import csu.web.credit_bank.utils.AliOssUtil;
import csu.web.credit_bank.utils.ImagePath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@Service
public class ImageServiceImpl implements ImageService {
    @Autowired
    private AliOssUtil aliOssUtil;

    private String getImageBasePath() {
        ApplicationHome home = new ApplicationHome(getClass());
        return home.getDir().getParentFile().getParentFile().getAbsolutePath()
                + File.separator + "src" + File.separator + "main"
                + File.separator + "resources" + File.separator + "images";
    }

    @Override
    public String saveImage(MultipartFile file) {
        try {
            return aliOssUtil.upload(file.getInputStream(),file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();

        }
        return "https://creditbank.oss-cn-chengdu.aliyuncs.com/2025/07/02/d608cb75-1f1c-48d1-819c-03152564aa22.png";
    }

    @Override
    public String saveImage(MultipartFile file, ImagePath imagePath) {
        try {
            // 1. 获取原始文件名并生成新的唯一文件名
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFilename ="new_"+ System.currentTimeMillis() + extension;

            // 2. 构建 static/images/ 的目标路径
            String projectRoot = new ApplicationHome(getClass()).getDir().getParentFile().getParent();
            String imageBasePath = projectRoot + "/src/main/resources/static/images/"+imagePath.getPath();

            // 3. 确保目录存在
            Path imageDir = Paths.get(imageBasePath);
            if (!Files.exists(imageDir)) {
                Files.createDirectories(imageDir);
            }

            // 4. 保存文件
            Path targetPath = imageDir.resolve(newFilename);
            file.transferTo(targetPath);

            // 5. 返回文件名（或相对路径）
            return imagePath.getPath()+newFilename;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "https://creditbank.oss-cn-chengdu.aliyuncs.com/2025/07/02/d608cb75-1f1c-48d1-819c-03152564aa22.png";
    }

    @Override
    public void deleteImage(String imageName) {

    }

    @Override
    public byte[] getImage(String imageName) {
        // TODO: implement this method



        return new byte[0];
    }

    @Override
    public boolean isImageExist(String imageName) {
        return false;
    }

    @Override
    public void updateImage(String oldImageName, String newImageName, byte[] imageData) {

    }
}
